<template>
  <div id="app">
    <!-- 登录和注册页面 -->
    <div v-if="$route.path === '/login' || $route.path === '/register'">
      <router-view />
    </div>
    
    <!-- 主应用页面 -->
    <el-container v-else>
      <el-header class="header">
        <div class="header-content">
          <div class="logo">
            <el-icon size="30"><School /></el-icon>
            <span class="title">栗子云</span>
          </div>
          <div class="user-info">
            <el-icon size="20"><User /></el-icon>
            <span>{{ username }}</span>
            <el-button
              type="text"
              size="small"
              @click="handleLogout"
              style="color: white; margin-left: 10px;"
            >
              退出
            </el-button>
          </div>
        </div>
      </el-header>
      
      <el-container>
        <el-aside width="200px" class="aside">
          <el-menu
            :default-active="$route.path"
            class="el-menu-vertical"
            background-color="#f5f7fa"
            text-color="#303133"
            active-text-color="#409EFF"
          >
            <el-menu-item index="/" v-if="isAdmin" @click="handleMenuClick('/')">
              <el-icon><House /></el-icon>
              <span>首页</span>
            </el-menu-item>
            <el-menu-item index="/user/info" @click="handleMenuClick('/user/info')">
              <el-icon><User /></el-icon>
              <span>用户信息</span>
            </el-menu-item>

          </el-menu>
        </el-aside>
        
        <el-main class="main">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { School, User, House } from '@element-plus/icons-vue'

export default {
  name: 'App',
  setup() {
    const router = useRouter()
    
    const username = computed(() => {
      const userInfo = localStorage.getItem('userInfo')
      if (userInfo) {
        try {
          const user = JSON.parse(userInfo)
          return user.username || '用户'
        } catch (e) {
          return '用户'
        }
      }
      return '用户'
    })
    
    // 获取用户角色
    const userRole = computed(() => {
      const userInfo = localStorage.getItem('userInfo')
      if (userInfo) {
        try {
          const user = JSON.parse(userInfo)
          return user.role || 'USER'
        } catch (e) {
          return 'USER'
        }
      }
      return 'USER'
    })
    
    // 是否为管理员
    const isAdmin = computed(() => {
      return userRole.value === 'ADMIN'
    })
    
    const handleLogout = () => {
      localStorage.removeItem('token')
      localStorage.removeItem('username')
      localStorage.removeItem('userInfo')
      ElMessage.success('已退出登录')
      router.push('/login')
    }
    
    const handleMenuClick = (path) => {
      // 权限检查：确保用户只能访问自己权限范围内的页面
      if (path === '/' && !isAdmin.value) {
        ElMessage.error('权限不足，无法访问首页')
        return
      }
      // 如果当前路径不同，则进行路由跳转
      if (router.currentRoute.value.path !== path) {
        router.push(path)
      }
    }
    
    return {
      username,
      userRole,
      isAdmin,
      handleLogout,
      handleMenuClick
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  height: 100vh;
}

.header {
  background-color: #409EFF;
  color: white;
  padding: 0;
  height: 60px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo .title {
  font-size: 24px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.aside {
  background-color: #f5f7fa;
  border-right: 1px solid #e4e7ed;
}

.el-menu-vertical {
  border-right: none;
}

.main {
  background-color: #ffffff;
  padding: 20px;
}

body {
  margin: 0;
  padding: 0;
}
</style>